package com.itheima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.itheima.kafka.pojo.User;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class HelloListener {

//    @KafkaListener(topics = {"kafka-hello"})
//    public void receiverMessage(ConsumerRecord<?, ?> record) {
//        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
//        if (optional.isPresent()) {
//            Object value = record.value();
//            System.out.println(value);
//        }
//    }

    // 或者使用
//    @KafkaListener(topics = {"kafka-hello"})
//    public void receiverMessage(String message) {
//        if (!StringUtils.isEmpty(message)) {
//            System.out.println(message);
//        }
//    }
    @KafkaListener(topics = {"kafka-hello"})
    public void receiverMessage(String message) {
        if(!StringUtils.isEmpty(message)){
            User user = JSON.parseObject(message, User.class);
            System.out.println(123);
            System.out.println(user);
        }
    }
}